package pl.smath.web.client;

import pl.smath.worksheet.ResolveExpression;
import pl.smath.worksheet.ResolveExpression.EquationType;
import pl.smath.worksheet.results.Resolution;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Web_demo implements EntryPoint {

	private InputExpressionWidget inputExpressionWidget = new InputExpressionWidget();
	private ResolutionWidget resolutionWidget = new ResolutionWidget();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		doLayout();
	}
	
	private void doLayout(){
	
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				processExpression();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					processExpression();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void processExpression() {
				ResolveExpression expression = new ResolveExpression();
				Resolution resolution = expression.resolve(inputExpressionWidget.getExpression(), EquationType.Simplify);
				resolutionWidget.setResolution(resolution);
				//refreshMathSymbols();
			}
		}
		
		RootPanel.get("input_expression_widget").add(inputExpressionWidget);
		RootPanel.get("resolution_widget").add(resolutionWidget);
		RootPanel.get("math_theory_widget").add(new MathTheoryWidget("uproszczenia"));
		
		MyHandler handler = new MyHandler();
		inputExpressionWidget.setClickHandler(handler);
		
		//refreshMathSymbols();
	}
	
	/**
	 * Odswiezenie wzorow matematycznych na stronie
	 */
	public static native void refreshMathSymbols() /*-{
	  $wnd.jsMath.ProcessBeforeShowing();
	}-*/;
	
}
